package com.cl.erp_2301.controller;

import com.cl.erp_2301.Service.SupplierService;
import com.cl.erp_2301.dto.DataDTO;
import com.cl.erp_2301.dto.MenuDTO;
import com.cl.erp_2301.dto.SupplierDTO;
import com.cl.erp_2301.entity.Supplier;
import com.cl.erp_2301.Service.SupplierService;
import com.cl.erp_2301.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/v2")
public class SupplierController {
    @Resource
    private SupplierService supplierService;

    @ResponseBody
    @RequestMapping("/supplier/listByPage")
    public Result listByPage(int page,int rows){
        Page<Object> page1=PageHelper.startPage(page, rows);
        List<SupplierDTO> supplierDTOList=supplierService.listByPage();
        DataDTO dataDTO=new DataDTO<>();
        dataDTO.setRows(supplierDTOList);
        dataDTO.setTotal(dataDTO.getTotal());
        return Result.ok(dataDTO);
    }

    @GetMapping("/supplier//listByPage")
    public Result listByPage2(int page,int rows){
        Page<Object> page1=PageHelper.startPage(page,rows);
        DataDTO data=new DataDTO<>();
        data.setRows(supplierService.listByPage());
        data.setTotal(page1.getTotal());
        return Result.ok(data);
    }
    @ResponseBody
    @RequestMapping("/supplier/add")
    public Result add(@RequestParam("name") String name,@RequestParam("address") String address,
                      @RequestParam("contact") String contact,@RequestParam("tele") String tele,
                      @RequestParam("email") String email){
        SupplierDTO supplierDTO=new SupplierDTO();
        supplierDTO.setName(name);
        supplierDTO.setAddress(address);
        supplierDTO.setContact(contact);
        supplierDTO.setTele(tele);
        supplierDTO.setEmail(email);
        supplierService.add(supplierDTO);
        return Result.ok();
    }
    @ResponseBody
    @RequestMapping("/supplier//add")
    public Result add2(@RequestParam("name") String name,@RequestParam("address") String address,
                      @RequestParam("contact") String contact,@RequestParam("tele") String tele,
                      @RequestParam("email") String email){
        SupplierDTO supplierDTO=new SupplierDTO();
        supplierDTO.setName(name);
        supplierDTO.setAddress(address);
        supplierDTO.setContact(contact);
        supplierDTO.setTele(tele);
        supplierDTO.setEmail(email);
        supplierService.add(supplierDTO);
        return Result.ok();
    }



}
